/*
 * PrinterSettings.cpp
 *
 * Created: 23.7.2013 9:44:36
 *  Author: Ferda
 */ 

#include <math.h>
 #include "PrinterSettings.h"
 #include "Configuration.h"
 #include "avr/eeprom.h"

//=====================================================================================
#if SYSTEM_CONFIG == 1 || SYSTEM_CONFIG == 2 || SYSTEM_CONFIG == 3

PrinterSettings EEMEM default_settings = 
{
	.ver = "000",
	.axis_steps_per_unit = _AXIS_STEP_PER_UNIT,
	.max_feedrate = _MAX_FEEDRATE,
	.max_acceleration_units_per_sq_second = _MAX_ACCELERATION_UNITS_PER_SQ_SECOND,
	.move_acceleration = _ACCELERATION,
	.retract_acceleration = _RETRACT_ACCELERATION,
	.minimumfeedrate = DEFAULT_MINIMUMFEEDRATE,
	.mintravelfeedrate = DEFAULT_MINTRAVELFEEDRATE,
	.min_seg_time = _MIN_SEG_TIME,
	.max_xy_jerk = fminf(JERK_DIV / default_settings.axis_steps_per_unit[0], JERK_DIV / default_settings.axis_steps_per_unit[1]),
	.max_z_jerk = JERK_DIV / default_settings.axis_steps_per_unit[2],
	.max_e_jerk = JERK_DIV / default_settings.axis_steps_per_unit[3],

	.PID_Kp = PID_PGAIN,
	.PID_Ki = PID_IGAIN,
	.PID_Kd = PID_DGAIN,
	.homing_feedrate = _HOMING_FEEDRATE,
	.extruder_offsets = { _EXTRUDER1_OFFSETS, _EXTRUDER2_OFFSETS },

	.stepper_inactive_time = _DEFAULT_STEPPER_INACTIVE_TIME,
	.max_inactive_time = _DEFAULT_MAX_INACTIVE_TIME,
	.change_tool_feedrate = _CHANGE_TOOL_FEEDRATE
};

PrinterSettings EEMEM stored_settings = {
	.ver = "001",
	.axis_steps_per_unit = _AXIS_STEP_PER_UNIT,
	.max_feedrate = _MAX_FEEDRATE,
	.max_acceleration_units_per_sq_second = _MAX_ACCELERATION_UNITS_PER_SQ_SECOND,
	.move_acceleration = _ACCELERATION,
	.retract_acceleration = _RETRACT_ACCELERATION,
	.minimumfeedrate = DEFAULT_MINIMUMFEEDRATE,
	.mintravelfeedrate = DEFAULT_MINTRAVELFEEDRATE,
	.min_seg_time = _MIN_SEG_TIME,
	.max_xy_jerk = fminf(JERK_DIV / default_settings.axis_steps_per_unit[0], JERK_DIV / default_settings.axis_steps_per_unit[1]),
	.max_z_jerk = JERK_DIV / default_settings.axis_steps_per_unit[2],
	.max_e_jerk = JERK_DIV / default_settings.axis_steps_per_unit[3],

	.PID_Kp = PID_PGAIN,
	.PID_Ki = PID_IGAIN,
	.PID_Kd = PID_DGAIN,
	.homing_feedrate = _HOMING_FEEDRATE,
	.extruder_offsets = { _EXTRUDER1_OFFSETS, _EXTRUDER2_OFFSETS },

	.stepper_inactive_time = _DEFAULT_STEPPER_INACTIVE_TIME,
	.max_inactive_time = _DEFAULT_MAX_INACTIVE_TIME,
	.change_tool_feedrate = _CHANGE_TOOL_FEEDRATE
	
};

#elif SYSTEM_CONFIG == 4

PrinterSettings EEMEM default_settings =
{
	.ver = "002",
	.PID_Kp = PID_PGAIN,
	.PID_Ki = PID_IGAIN,
	.PID_Kd = PID_DGAIN
};

PrinterSettings EEMEM stored_settings = {
	.ver = "003",
	.PID_Kp = PID_PGAIN,
	.PID_Ki = PID_IGAIN,
	.PID_Kd = PID_DGAIN
};


#endif

//=====================================================================================================

PrinterSettings settings;

void loadSettings(uint8_t def)
{
	const void * ptr = 0;
	
	if (def != 0)
		ptr = &default_settings;
	else
		ptr = &stored_settings;
	
	eeprom_read_block(&settings, ptr, sizeof(PrinterSettings));
}

void storeSettings()
{
	eeprom_update_block(&settings, (void*)(&stored_settings), sizeof(PrinterSettings));
}

void printSettings()
{

}